package st.lib.base.delegate.bottom;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import com.joanzapata.iconify.widget.IconTextView;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

import butterknife.BindView;
import me.yokeyword.fragmentation.ISupportFragment;
import me.yokeyword.fragmentation.SupportFragment;
import st.lib.base.R;
import st.lib.base.R2;
import st.lib.base.delegate.ShadowDelegate;

/**
 * @author zhangyu
 * @date 2017/10/27
 */

public abstract class BaseBottomDelegate extends ShadowDelegate implements View.OnClickListener {
    private final ArrayList<BottomTitleBean> TAB_BEANS = new ArrayList<>();
    private final ArrayList<BottomItemDelegate> ITEM_DELEGATES = new ArrayList<>();
    private final LinkedHashMap<BottomTitleBean, BottomItemDelegate> ITEMS = new LinkedHashMap<>();
    private int mCurrentDelegate = 0;
    private int mIndexDelegate = 0;
    private int mClickColor = Color.RED;
    private int mDefaultColor = Color.GRAY;
    private int mDefaultBackground = Color.WHITE;

    public abstract LinkedHashMap<BottomTitleBean,BottomItemDelegate> setItems(ItemBuilder builder);

    public abstract int setIndexDelegate();

    @ColorInt
    public abstract int setClickColor();
    @ColorInt
    public abstract int setBackground();


    @BindView(R2.id.layout_bottom_bar)
    LinearLayoutCompat mLayoutBottomBar;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mIndexDelegate = setIndexDelegate();
        if (setClickColor() != 0) {
            mClickColor = setClickColor();
        }
        if (setDefaultColor() != 0) {
            mDefaultColor = setDefaultColor();
        }
        if(setBackground() != 0){
            mDefaultBackground = setBackground();
        }

        final ItemBuilder itemBuilder =ItemBuilder.builder();
        final LinkedHashMap<BottomTitleBean,BottomItemDelegate> items = setItems(itemBuilder);
        if (items != null) {
            ITEMS.putAll(items);
        }
        for (Map.Entry<BottomTitleBean, BottomItemDelegate> item : ITEMS.entrySet()) {
            final BottomTitleBean bottomTitleBean = item.getKey();
            final BottomItemDelegate bottomItemDelegate = item.getValue();
            TAB_BEANS.add(bottomTitleBean);
            ITEM_DELEGATES.add(bottomItemDelegate);
        }
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_bottom;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mLayoutBottomBar.setBackgroundColor(mDefaultBackground);
        int size = ITEMS.size();
        for (int i = 0; i < size; i++) {
            LayoutInflater.from(getContext()).inflate(R.layout.item_bottom_icon_text, mLayoutBottomBar);
            RelativeLayout item = (RelativeLayout) mLayoutBottomBar.getChildAt(i);
            //设置item的点击事件
            item.setTag(i);
            item.setOnClickListener(this);
            //给每个item赋值
            final IconTextView tvIcon = (IconTextView) item.getChildAt(0);
            final AppCompatTextView tvTitle = (AppCompatTextView) item.getChildAt(1);
            final BottomTitleBean bean = TAB_BEANS.get(i);
            tvIcon.setText(bean.getIcon());
            tvTitle.setText(bean.getTitle());
            if (i == mIndexDelegate) {
                tvIcon.setTextColor(mClickColor);
                tvTitle.setTextColor(mClickColor);
            } else {
                tvIcon.setTextColor(mDefaultColor);
                tvTitle.setTextColor(mDefaultColor);
            }
        }
        final ISupportFragment[] delegateArray = ITEM_DELEGATES.toArray(new SupportFragment[size]);
        getSupportDelegate().loadMultipleRootFragment(R.id.layout_container, mIndexDelegate, delegateArray);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    /**
     * 重置按钮状态
     */
    private void resetColor() {
        for (int i = 0, size = mLayoutBottomBar.getChildCount(); i < size; i++) {
            final RelativeLayout item = (RelativeLayout) mLayoutBottomBar.getChildAt(i);
            final IconTextView tvIcon = (IconTextView) item.getChildAt(0);
            tvIcon.setTextColor(mDefaultColor);
            final AppCompatTextView tvTitle = (AppCompatTextView) item.getChildAt(1);
            tvTitle.setTextColor(mDefaultColor);
        }
    }

    @Override
    public void onClick(View v) {
        resetColor();
        final int tag = (int) v.getTag();
        final RelativeLayout item = (RelativeLayout) v;
        final IconTextView tvIcon = (IconTextView) item.getChildAt(0);
        tvIcon.setTextColor(mClickColor);
        final AppCompatTextView tvTitle = (AppCompatTextView) item.getChildAt(1);
        tvTitle.setTextColor(mClickColor);
        showHideFragment(ITEM_DELEGATES.get(tag), ITEM_DELEGATES.get(mCurrentDelegate));
        mCurrentDelegate = tag;
    }

    @ColorInt
    protected int setDefaultColor() {
        return Color.GRAY;
    }

//    public void onStartBotherFragment(StartBrotherFragmentEvent event){
//        start(event.mBaseDelegate);
//    }
}
